<style scoped>
    .summary-details-page{}
</style>

<template>
    <div class="summary-details-page">
        <!-- 计划明细 -->
        <el-form :inline="true" :model="formData" class="demo-form-inline">
            <el-form-item label="法人名称" class="selectMB10">
                <el-select size="mini" v-model="formData.legalPersonId" placeholder="请选择" @change="getLegalPersonCode">
                    <el-option v-for="(legalPerson,index) in legalPersonDropDown" :key="index" :label="legalPerson.legalPersonName":value="legalPerson.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="计划汇总号" class="selectMB10">
                <el-input size="mini" disabled v-model="formData.no" placeholder=""></el-input>
            </el-form-item>
            <el-form-item label="供应商名称" class="selectMB10">
                <el-select size="mini" v-model="formData.vendorName"  @change="supplierNameDropDown" placeholder="请选择">
                    <el-option v-for="(supplier,index) in supplierList" :key="index" :label="supplier.vendorName" :value="supplier.id"></el-option>
                </el-select>
            </el-form-item>
             <el-form-item label="备件编号" class="selectMB10">
                <el-input size="mini" v-model="formData.materialCode" placeholder="请输入备件编号"></el-input>
            </el-form-item>
             <el-form-item label="备件名称" class="selectMB10">
                <el-input size="mini" v-model="formData.materialName" placeholder="请输入备件名称"></el-input>
            </el-form-item>
             <el-form-item label="替换状态" class="selectMB10">
                <el-select clearable  size="mini" v-model="formData.replaceStatus" placeholder="请选择" style="width:100px;">
                    <el-option v-for="(replaceStatus,index) in formData.replaceStatusList" :key="index" :label="replaceStatus.label":value="replaceStatus.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item class="selectMB10">
               <el-button class="bolder outsideBtnBS selectAll" :disabled="selectDisabled" size="mini" type="danger" @click="planSummaryQuery(pageIndex,pageSize)">查询</el-button>
            </el-form-item>
            <el-form-item class="selectMB10" v-if="powerFlag2">
               <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="createPO">
                    创建PO
               </el-button>
            </el-form-item>
        </el-form>
         <el-table ref="clearSel" :data="spareLists" border style="width: 100%" v-loading.body="loading" element-loading-text="拼命加载中"@selection-change="handleSelectionChange">
             <el-table-column type="index" header-align="center" align="center" label="序"  width="40"></el-table-column>
             <el-table-column type="selection"  width="40" align="center" :selectable="selectable"></el-table-column>
             <!--
             <el-table-column prop="selectItem" label="选择" header-align="center" align="center" width="40">
                <template scope="scope">
                    <el-checkbox size="mini" v-if="scope.row.replaceStatus==1" disabled :label="scope.row.id">&nbsp;</el-checkbox>
                    <el-checkbox size="mini" v-else :label="scope.row.id"  @change="selectMateraial(scope.$index, scope.row,$event)">&nbsp;</el-checkbox>
                </template>
            </el-table-column>
            -->
            <el-table-column label="操作" header-align="center" align="center" width="160" v-if="powerFlag1 || powerFlag3">
                <template scope="scope">
                    <div class="operationItem bolder disabled" v-if="scope.row.replaceStatus==1 && powerFlag1">替换</div>
                    <div class="operationItem bolder" v-else-if="scope.row.replaceStatus!=1 && powerFlag1" @click="replace(scope.$index, scope.row)">替换</div>
                    <div class="operationItem bolder disabled" v-if="scope.row.replaceStatus==1 && powerFlag3">更改供应商</div>
                    <div class="operationItem bolder" v-else-if="scope.row.replaceStatus!=1 && powerFlag3" @click="replacementVendor(scope.$index, scope.row)">更改供应商</div>
                </template>
            </el-table-column>
            <el-table-column label="备件编码" header-align="center" width="160" show-overflow-tooltip>
                <template scope="scope">
                    <el-button size="mini" v-if="scope.row.replaceStatus==1" disabled @click="checkTheDetails(scope.$index, scope.row)"   class="blueFont">{{scope.row.materialCode}}</el-button>
                    <el-button size="mini" v-else @click="checkTheDetails(scope.$index, scope.row)"  class="blueFont">{{scope.row.materialCode}}</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="materialName" label="备件名称" header-align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="standard" label="规格" header-align="center" width="80" show-overflow-tooltip></el-table-column>
            <el-table-column prop="unit" label="计量单位" header-align="center" width="80" show-overflow-tooltip></el-table-column>
            <el-table-column prop="quantity" label="计划数量" header-align="center" width="80"></el-table-column>
            <el-table-column prop="receivedQuantity" label="未采购数量" header-align="center" width="80">
                <template scope="scope">
                    {{scope.row.receivedQuantity}}
                </template>
            </el-table-column>
            <el-table-column prop="amount" label="已采购数量" header-align="center" width="80"></el-table-column>
            <el-table-column prop="replaceStatusName" label="替换状态" header-align="center" align="center" width="80" show-overflow-tooltip></el-table-column>
        </el-table>
        <!--分页-->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"  :current-page.sync="currentPage3" :current-page="pageIndex"  :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :page-count="totalPage" :total="totalNumber"></el-pagination>
    
        <!-- 
        <el-collapse v-model="activeNames" @change="handleChange">
            <el-collapse-item :title=" '汇总编号：'+item.code+'    供应商：'+item.gys" :name="item.id" v-for="item in dateLists">
                <el-table :data="item.spareLists" border style="width: 100%">
                    <el-table-column type="index" label="序" header-align="center" align="center" width="40"></el-table-column>
                    <el-table-column prop="selectItem" label="选择" header-align="center" align="center" width="60">
                        <template scope="scope">
                            <el-checkbox size="mini" :label="scope.row.id"></el-checkbox>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" header-align="center" align="center" width="180">
                        <template scope="scope">
                            <el-button size="mini" type="danger" @click="replace(scope.$index, scope.row)">替换</el-button>
                            <el-button size="mini" type="danger"  @click="replacementVendor(scope.$index, scope.row)">更改供应商</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column prop="code" label="备件编码" header-align="center" width="120"></el-table-column>
                    <el-table-column prop="name" label="备件名称" header-align="center"></el-table-column>
                    <el-table-column prop="gg" label="规格" header-align="center" width="80"></el-table-column>
                    <el-table-column prop="jldw" label="计量单位" header-align="center" width="80"></el-table-column>
                    <el-table-column prop="jhsl" label="计划数量" header-align="center" width="80"></el-table-column>
                    <el-table-column prop="wcgsl" label="未采购数量" header-align="center" width="80"></el-table-column>
                    <el-table-column prop="ycgsl" label="已采购数量" header-align="center" width="80"></el-table-column>
                    <el-table-column prop="thzt" label="替换状态" header-align="center" align="center" width="80"></el-table-column>
                </el-table>
            </el-collapse-item>
        </el-collapse>
        -->
        <!-- 替换的弹窗 -->
        <el-dialog id="replaceMask" title="备件替换" :visible.sync="replaceVisible" size="large" :lock-scroll="false">
            <el-form :inline="true" :model="spareForm" ref="spareForm" class="demo-form-inline">
                <el-form-item label="计划备件编号" :label-width="formLabelWidth">
                    <el-input size="mini" disabled v-model="spareForm.originalMaterialCode" auto-complete="off" placeholder="当前的备件编号"></el-input>
                </el-form-item>
                <el-form-item label="计划备件名称" :label-width="formLabelWidth">
                    <el-input size="mini" disabled v-model="spareForm.originalMaterialName" auto-complete="off" placeholder="当前的备件名称"></el-input>
                </el-form-item>
                <el-form-item label="替换备件编号" :label-width="formLabelWidth" id="repalceSpareCode" prop="replaceMaterialCode"   :rules="[{ required: true, message: '替换备件编号不能为空'}]">
                    <el-input size="mini" v-model.trim="spareForm.replaceMaterialCode" auto-complete="off" placeholder="请输入备件编号"></el-input>
                </el-form-item>
                <el-form-item label="替换备件名称" :label-width="formLabelWidth" prop="replaceMaterialName"   :rules="[{ required: true, message: '替换备件名称不能为空'}]">
                    <el-input size="mini" disabled v-model="spareForm.replaceMaterialName" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="替换备件Topmost" :label-width="formLabelWidth">
                    <el-input size="mini" disabled v-model="spareForm.topMostText" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <el-table :data="planItemList" border style="width: 100%">
                <el-table-column type="index" header-align="center" align="center" label="序"  width="40"></el-table-column>
                <el-table-column property="planCode" label="计划编码" header-align="center" show-overflow-tooltip> </el-table-column>
                <el-table-column label="备件编号" header-align="center" show-overflow-tooltip>
                    <template scope="scope">
                        <div v-if="!scope.row.replaceFlag">{{ scope.row.materialCode }}</div>
                        <el-popover v-else trigger="hover" placement="top">
                            <p>原备件编码: {{ scope.row.oldMaterialCode}}</p>
                            <p>原备件名称: {{ scope.row.oldMaterialName}}</p>
                            <p>现备件编码: {{ scope.row.materialCode}}</p>
                            <p>现备件名称: {{ scope.row.materialName}}</p>
                            <div slot="reference" class="name-wrapper">
                                <el-tag>{{ scope.row.materialCode }}</el-tag>
                            </div>
                        </el-popover>
                    </template>

                </el-table-column>
                <el-table-column property="materialName" label="备件名称" header-align="center" show-overflow-tooltip></el-table-column>
                <el-table-column property="quantity" label="计划数量" header-align="center"></el-table-column>
                <el-table-column property="deliveryTimeExpectation" label="期望到货日期" header-align="center" show-overflow-tooltip></el-table-column>
                <el-table-column property="remark" label="备注" header-align="center" show-overflow-tooltip></el-table-column>
                <el-table-column property="maxQuantity" label="未采购数量" header-align="center"></el-table-column>
                <el-table-column property="allottedQuantity" label="替换数量" header-align="center" width="80"  :rules="[{ type: 'number', message: '替换数量必须为数字值'}]">
                     <template scope="scope">
                        <el-input size="mini" class="allottedQuantity" v-model.number="scope.row.allottedQuantity" placeholder="请输入" @change="replaceQuantity(scope.$index, scope.row)"></el-input>
                    </template>
                </el-table-column>
            </el-table>
            <span slot="footer" class="dialog-footer">
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="determineReplace()">确定</el-button>
            </span>
        </el-dialog>
        <!-- 更换供应商的弹窗 -->
        <el-dialog id="replacementVendorMask" title="更改供应商" :visible.sync="replacementVendorVisible" size="large" :lock-scroll="false">
            <el-form :inline="true" :model="replacementVendorForm" class="demo-form-inline">
                <el-form-item label="供应商编号" :label-width="formLabelWidth">
                    <el-input size="mini" v-model="replacementVendorForm.supplierCode" auto-complete="off" placeholder="请输入供应商编号"></el-input>
                </el-form-item>
                <el-form-item label="供应商名称" :label-width="formLabelWidth">
                    <el-input size="mini" v-model="replacementVendorForm.supplierName" auto-complete="off" placeholder="请输入供应商名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button class="bolder outsideBtnBS selectAll" size="mini" type="danger" @click="queryVendor">查询</el-button>
                    <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="confirmChangeSupplier">确定</el-button>
                </el-form-item>
            </el-form>
            <el-table :data="supplierData" border style="width: 100%">
                <el-table-column type="index" header-align="center" align="center" label="序"  width="40"></el-table-column>
                <el-table-column property="" label="选择" header-align="center" align="center" width="40">
                    <template scope="scope">
                        <el-radio class="radio"  v-model="selectSupplierId" :label="scope.row.id" >&nbsp;</el-radio>
                    </template>
                </el-table-column>
                <el-table-column property="partnerCode" label="供应商编号" header-align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column property="partnerName" label="供应商名称" header-align="center" show-overflow-tooltip></el-table-column>
                <el-table-column property="activePrice" label="有效不含税价格" header-align="center"></el-table-column>
                <el-table-column property="includingTaxPrice" label="有效含税价格" header-align="center"></el-table-column>
                <el-table-column property="changedAllQuantity" label="本次更改数量" header-align="center">
                    <template scope="scope">
                        <span v-if="selectSupplierId == scope.row.id">{{changedAllQuantity}}</span>
                    </template>
                </el-table-column>
            </el-table>
            <h2 style="margin-top:10px;">计划明细：</h2>
            <el-table :data="planDetailsData" border style="width: 100%">
                <el-table-column type="index" header-align="center" align="center" label="序"  width="40"></el-table-column>
                <el-table-column property="planCode" label="计划编码" header-align="center" width="100" show-overflow-tooltip> </el-table-column>
                <el-table-column  label="备件编号" width="150" header-align="center" show-overflow-tooltip>
                    <template scope="scope">
                        <div v-if="!scope.row.replaceFlag">{{ scope.row.materialCode }}</div>
                        <el-popover v-else trigger="hover" placement="top">
                            <p>原备件编码: {{ scope.row.oldMaterialCode}}</p>
                            <p>原备件名称: {{ scope.row.oldMaterialName}}</p>
                            <p>现备件编码: {{ scope.row.materialCode}}</p>
                            <p>现备件名称: {{ scope.row.materialName}}</p>
                            <div slot="reference" class="name-wrapper">
                                <el-tag>{{ scope.row.materialCode }}</el-tag>
                            </div>
                        </el-popover>
                    </template>
                </el-table-column>
                <el-table-column property="materialName" label="备件名称" header-align="center" show-overflow-tooltip></el-table-column>
                <el-table-column property="quantity" label="计划数量" header-align="center"></el-table-column>
                <el-table-column property="deliveryTimeExpectation" label="期望到货日期" header-align="center" show-overflow-tooltip></el-table-column>
                <el-table-column property="remark" label="备注" header-align="center" show-overflow-tooltip></el-table-column>
                <el-table-column property="maxQuantity" label="未采购数量" header-align="center"></el-table-column>
                <el-table-column property="replaceQuantity" label="本次更改数量" header-align="center" :rules="[{ type: 'number', message: '本次更改数量必须为数字值'}]">
                    <template scope="scope">
                        <el-input size="mini" class="replaceQuantity" v-model.number="scope.row.replaceQuantity"  @change="changedQuantity(scope.$index, scope.row)"></el-input>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 
            <span slot="footer" class="dialog-footer">
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="">确定</el-button>
            </span>
            -->
        </el-dialog>
        <!-- 点击每行的备件编码查看明细 明细弹窗 -->
        <el-dialog title="明细" :visible.sync="checkTheDetailsVisible" size="large" :lock-scroll="false">
            <el-table :data="detailsData" border style="width: 100%">
                <el-table-column type="index" header-align="center" align="center" label="序"  width="40"></el-table-column>
                <el-table-column property="planCode" label="计划编号" header-align="center" show-overflow-tooltip> </el-table-column>
                <el-table-column property="source" label="计划来源" header-align="center" show-overflow-tooltip> </el-table-column>
                <el-table-column property="planCategory" label="计划分类" header-align="center" show-overflow-tooltip> </el-table-column>
                <el-table-column label="备件编号" width="150" header-align="center" show-overflow-tooltip>
                    <template scope="scope">
                        <div v-if="!scope.row.replaceFlag">{{ scope.row.materialCode }}</div>
                        <el-popover v-else trigger="hover" placement="top">
                            <p>原备件编码: {{ scope.row.oldMaterialCode}}</p>
                            <p>原备件名称: {{ scope.row.oldMaterialName}}</p>
                            <p>现备件编码: {{ scope.row.materialCode}}</p>
                            <p>现备件名称: {{ scope.row.materialName}}</p>
                            <div slot="reference" class="name-wrapper">
                                <el-tag>{{ scope.row.materialCode }}</el-tag>
                            </div>
                        </el-popover>
                    </template>
                </el-table-column>
                <el-table-column property="materialName" label="备件名称" header-align="center" show-overflow-tooltip></el-table-column>
                <el-table-column property="standard" label="规格" header-align="center" show-overflow-tooltip> </el-table-column>
                <el-table-column property="unit" label="计量单位" header-align="center" show-overflow-tooltip> </el-table-column>
                <el-table-column property="quantity" label="计划数量" header-align="center"></el-table-column>
                <el-table-column property="price" label="单价" header-align="center" align="right"> </el-table-column>
                <el-table-column property="amount" label="计划总金额" header-align="center" align="right"> </el-table-column>
                <el-table-column property="deliveryTimeExpectation" label="期望到货日期" header-align="center" show-overflow-tooltip></el-table-column>
                <el-table-column property="remark" label="备注" header-align="center" show-overflow-tooltip></el-table-column>
            </el-table>
        </el-dialog>
    </div>
    
    
</template>
<script>
    export default  {
        data() {
            return {
                // activeNames: 1,
                legalPersonDropDown:[],  //法人名称下拉列表
                planAggregationId:"",    //继续开单时传过来的
                planAggregationItemId:"",  //开制PO时传过来的
                continueBillingOpenSystemPOFlag:true,   //继续开单为true,开制PO为false
                formData:{
                    no:"",   //  计划汇总号
                    legalPersonId:"",
                    legalPersonCode:"",
                    planSummaryNumber:"",
                    vendorName:"",
                    vendorId:"",
                    materialCode:"",
                    materialName:"",
                    replaceStatus:""
                },
                supplierList:[],   //供应商名称下拉列表
                loading:false,
                spareLists:[],
                pageIndex:1,    //当前页（必传）
                pageSize :10,   //每页多少条
                currentPage3:3,    //当前显示3页
                
                totalNumber: 20,     //总条数
                totalPage:2,    //总页数
                replaceVisible:false,     //替换的弹窗
                spareForm:{       // 备件信息
                    originalMaterialCode:"",
                    originalMaterialName:"",
                    replaceMaterialCode:"",
                    replaceMaterialName:"",
                    topMostText:"",    // 备件替换Topmost
                    priority:""   // 采购优先级
                },
                formLabelWidth: "90",
                planItemList:[],  //备件替换数据
                replacementVendorVisible:false,   //更换供应商的弹窗
                replacementVendorForm:{
                    supplierCode:"",
                    supplierName:""
                },
                supplierData:[],    // 更改供应商弹窗中  供应商列表
                supplierDataFull:[],    // 更改供应商弹窗中  供应商列表
                selectSupplierId:"",   // 选中的供应商id
                selectSupplierName:"",  // 选中的供应商名称
                changedAllQuantity:0,   //本次更改的总数量
                activePrice:"",   //选中供应商的价格
                planDetailsData:[],   // 更改供应商弹窗中  计划明细列表
                confirmChangeSupplierPlanItemList:[],   //确认更改供应商 的计划行项目
                checkTheDetailsVisible:false,    // 点击每行的备件编码查看明细
                detailsData:[],     // 点击每行的备件编码查看明细

                planAggregationItemIds:[],    //点击创建PO ,传的参数
                vendorId:"",  //点击创建PO ,传的参数
                // legalPersonCode:""    //点击创建PO ,传的参数
                //权限
                powerFlag1:false,
                powerFlag2:false,
                powerFlag3:false,
                //查询禁用
                selectDisabled:false
            };
        },
        computed:{
            powerData(){
                return this.$store.state.powerData;
            }
        },
        watch:{
            powerData(){
                var _this=this;
                for(var i=0;i<_this.powerData.length;i++){
                    if(_this.powerData[i]=="ROLE_security_planDistribution_replace"){
                        //计划分配替换权限
                        _this.powerFlag1=true;
                    }
                    if(_this.powerData[i]=="ROLE_security_planDistribution_createPO"){
                        //创建po权限
                        _this.powerFlag2=true;
                    }
                    if(_this.powerData[i]=="ROLE_security_planDistribution_changeSupplier"){
                        //计划分配更改供应商权限
                        _this.powerFlag3=true;
                    }
                }
            }
        },
        mounted:function(){
            var _this=this;
            _this.$store.commit('currentActive', 'SummaryDetails');
            // //ajax前后查询按钮禁用
            // $(".selectAll").ajaxStart(function(){
            //     _this.selectDisabled=true;
            // });
            // $(".selectAll").ajaxStop(function(){
            //     _this.selectDisabled=false;
            // });
            _this.planAggregationId=_this.$route.query.planAggregationId;   // 继续开单
            _this.planAggregationItemId=_this.$route.query.planAggregationItemId;   // 开制PO
            _this.formData.no=_this.$route.query.no;   // 计划汇总号
            // 判断是从继续开单过来还是开制PO过来
            if(_this.$route.query.planAggregationId){
                _this.continueBillingOpenSystemPOFlag=true;
            }else if(_this.$route.query.planAggregationItemId){
                _this.continueBillingOpenSystemPOFlag=false;
            }
            if(_this.continueBillingOpenSystemPOFlag){ //继续开单
                _this.legalPersonDropDownValue(); //法人名称下拉列表值
                _this.supplierLists();     //供应商下拉列表中的值
            }else{   //开制PO
                _this.legalPersonDropDownValue1(); //法人名称下拉列表值
                _this.supplierLists1();     //供应商下拉列表中的值
            }

            
            _this.replaceStatusLists();     //替换状态下拉列表中的值
            // _this.planSummaryQuery(_this.pageIndex,_this.pageSize);   //汇总计划详情界面查询
            //判断权限
            for(var i=0;i<_this.powerData.length;i++){
                if(_this.powerData[i]=="ROLE_security_planDistribution_replace"){
                    //计划分配替换权限
                    _this.powerFlag1=true;
                }
                if(_this.powerData[i]=="ROLE_security_planDistribution_createPO"){
                    //创建po权限
                    _this.powerFlag2=true;
                }
                if(_this.powerData[i]=="ROLE_security_planDistribution_changeSupplier"){
                    //计划分配更改供应商权限
                    _this.powerFlag3=true;
                }
            }

            
        },
        methods:{
            selectAll(){
                var _this=this;
                var canSelectedLists=[];
                for(var i=0;i<_this.spareLists.length;i++){
                    if(_this.spareLists[i].replaceStatus != 1){ //_this.spareLists.replaceStatus状态为1时，不可选
                        canSelectedLists.push(_this.spareLists[i]);
                    }
                }
                for(var i=0;i<canSelectedLists.length;i++){
                    _this.$refs.clearSel.toggleRowSelection(canSelectedLists[i]);//默认全选(已替换的不可选)
                }
            },
            //继续开单-------法人名称下拉列表值
            legalPersonDropDownValue(){  
                var _this = this;
                var tmp={planAggregationId:_this.planAggregationId};
                $.ajax({
                    url:_this.procurement_service+"planAggregation/queryLegalPersonsByAggregationId",
                    type: 'POST',
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success:function(data){
                        if(data.success){
                            _this.legalPersonDropDown = data.data;
                            if(_this.legalPersonDropDown != null && _this.legalPersonDropDown.length != 0){
                                _this.formData.legalPersonId = data.data[0].id;
                                _this.formData.legalPersonCode = data.data[0].legalPersonCode;
                            }
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            //开制PO---------法人名称下拉列表值
            legalPersonDropDownValue1(){  
                var _this = this;
                var tmp={planAggregationItemId:_this.planAggregationItemId};
                $.ajax({
                    url:_this.procurement_service+"planAggregation/queryLegalPersonsByAggregationItemId",
                    type: 'POST',
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success:function(data){
                        if(data.success){
                            _this.legalPersonDropDown = data.data;
                            if(_this.legalPersonDropDown != null && _this.legalPersonDropDown.length != 0){
                                _this.formData.legalPersonId = data.data[0].id;
                                _this.formData.legalPersonCode = data.data[0].legalPersonCode;
                            }
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            //取法人的code
            getLegalPersonCode(){
                var _this=this;
                for(var i=0;i<_this.legalPersonDropDown.length;i++){
                    if(_this.formData.legalPersonId == _this.legalPersonDropDown[i].id){
                        _this.formData.legalPersonCode = _this.legalPersonDropDown[i].legalPersonCode;
                    }
                }
            },
            //继续开单---------供应商下拉列表中的值
            supplierLists(){
                var _this=this;
                var tmp={planAggregationId:_this.planAggregationId};
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"planAggregation/queryProvidersByAggregationId",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",     //*****
                    success: function (data) {
                        if (data.success) {
                             _this.supplierList=data.data;
                             if(_this.supplierList != null && _this.supplierList.length != 0){
                                 _this.formData.vendorName=data.data[0].vendorName;
                                 _this.formData.vendorId=data.data[0].id;
                             }
                            _this.planSummaryQuery(_this.pageIndex,_this.pageSize);   //汇总计划详情界面查询
                        }else
                            // console.log(data.errorMessage);                             
                            _this.common.errorTip(data.errorMessage);
                    }
                });
            },
            // 开制PO ---------供应商下拉列表中的值
            supplierLists1(){
                var _this=this;
                var tmp={planAggregationItemId:_this.planAggregationItemId};
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"planAggregation/queryProvidersByAggregationItemId",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",     //*****
                    success: function (data) {
                        if (data.success) {
                            _this.supplierList=data.data;
                            if(_this.supplierList != null && _this.supplierList.length != 0){
                                _this.formData.vendorName=data.data[0].vendorName;
                                _this.formData.vendorId=data.data[0].id;
                            }
                            _this.planSummaryQuery(_this.pageIndex,_this.pageSize);   //汇总计划详情界面查询
                        }else
                            // console.log(data.errorMessage);                             
                            _this.common.errorTip(data.errorMessage);
                    }
                });
            },
            // 替换状态下拉列表中的值
            replaceStatusLists(){
                 var _this=this;
                $.ajax({
                    type: 'GET',
                    url: _this.procurement_service+"materialReplaceAffirm/searchInitParameter",
                    cache: false,
                    // data:JSON.stringify(tmp),
                    // dataType:'JSON',
                    // contentType: "application/json;charset=utf-8",     //*****
                    success: function (data) {
                        if (data.success) {
                            _this.formData.replaceStatusList=data.data.statusList;
                        }else
                            // console.log(data.errorMessage);                             
                            _this.common.errorTip(data.errorMessage);
                    }
                });
            },
            // 下拉列表选择供应商
            supplierNameDropDown(){
                var _this=this;
                for(var i=0;i<_this.supplierList.length;i++){
                    if(_this.formData.vendorName == _this.supplierList[i].id){
                        _this.formData.vendorId=_this.supplierList[i].id;
                    }
                }
            }, 
            //汇总计划详情界面查询     查询按钮 
            planSummaryQuery(pageIndex,pageSize){
                var _this=this;
                _this.loading=true;
                if(_this.continueBillingOpenSystemPOFlag){   //汇总明细、继续开单
                    var tmp={legalPersonId:_this.formData.legalPersonId,planAggregationId:_this.planAggregationId,vendorId:_this.formData.vendorId,pageIndex:pageIndex,pageSize :pageSize,materialName:_this.formData.materialName,materialCode:_this.formData.materialCode,replaceStatus:_this.formData.replaceStatus};
                    _this.selectDisabled=true;
                    $.ajax({
                        type: 'POST',
                        url: _this.procurement_service+"planAggregation/queryAggregationItemListByAggregationId",
                        cache: false,
                        data:JSON.stringify(tmp),
                        dataType:'JSON',
                        contentType: "application/json;charset=utf-8",     //*****
                        success: function (data) {
                            if (data.success) {
                                _this.loading=false;
                                _this.spareLists=data.data.dataList;
                                 _this.$nextTick(function(){
                                    _this.selectAll();
                                })

                                // 计算未采购数量
                                for(var i=0;i<_this.spareLists.length;i++){
                                    _this.spareLists[i].receivedQuantity = _this.spareLists[i].quantity - _this.spareLists[i].amount;
                                }
                                _this.totalNumber=data.data.totalNumber;    //总条数
                                _this.totalPage=data.totalPage;    //总页数
                            }else{
                                _this.common.errorTip(data.errorMessage);
                            }    
                            _this.selectDisabled=false;   
                        }
                    });
                }else{    //开制PO
                    var tmp={planAggregationItemId:_this.planAggregationItemId,vendorId:_this.formData.vendorId,pageIndex:pageIndex,pageSize :pageSize};
                    $.ajax({
                        type: 'POST',
                        url: _this.procurement_service+"planAggregation/queryAggregationItemListByAggregationItemId",
                        cache: false,
                        data:JSON.stringify(tmp),
                        dataType:'JSON',
                        contentType: "application/json;charset=utf-8",     //*****
                        success: function (data) {
                            if (data.success) {
                                _this.loading=false;
                                _this.spareLists=data.data.dataList;
                                _this.$nextTick(function(){
                                    _this.selectAll();
                                })
                                // 计算未采购数量
                                for(var i=0;i<_this.spareLists.length;i++){
                                    _this.spareLists[i].receivedQuantity = _this.spareLists[i].quantity - _this.spareLists[i].amount;
                                }
                                _this.totalNumber=data.data.totalNumber;    //总条数
                                _this.totalPage=data.totalPage;    //总页数
                            }else
                                // console.log(data.errorMessage);    
                                _this.common.errorTip(data.errorMessage);
                        }
                    });
                }
            },
            // handleChange(){
            //     console.log();
            // },
            // 点击每行的备件编码查看明细
            checkTheDetails(index,row){
                // console.log(index,row);
                var _this=this;
                _this.checkTheDetailsVisible=true;
                _this.planAggregationItemId=row.id;
                var tmp={planAggregationItemId:_this.planAggregationItemId};
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"planAggregation/queryPlanItemsByAggregationItemId",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",     //*****
                    success: function (data) {
                        if (data.success) {
                            // console.log(data.data);
                            var arr=data.data;
                            //单价保留八位小数，总金额保留两位
                            for(var i=0;i<arr.length;i++){
                                arr[i].price=arr[i].price.toFixed(8);
                                arr[i].amount=arr[i].amount.toFixed(2);
                            }
                            _this.detailsData=arr;
                        }else
                            // console.log(data.errorMessage);                             
                            _this.common.errorTip(data.errorMessage);
                    }
                });
            },
            // 替换
            replace(index,row){
                // console.log(index,row);
                var _this=this;
                _this.replaceVisible=true;
                _this.planAggregationItemId=row.id;
                _this.spareForm.originalMaterialCode=row.materialCode;
                _this.spareForm.originalMaterialName=row.materialName;
                _this.spareForm.unit=row.unit;

                _this.spareForm.replaceMaterialCode=null;
                _this.spareForm.replaceMaterialName=null;
                _this.spareForm.topMostText=null;   // 备件替换Topmost
                _this.spareForm.priority=null;    // 采购优先级
                
                $(".allottedQuantity").find("input").val(0);   //  备件替换弹窗中 替换数量为0 （置空）

                var tmp={planAggregationItemId:_this.planAggregationItemId};
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"planAggregation/queryInfoByAggregationItemId",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",     //*****
                    success: function (data) {
                        if (data.success) {
                            // console.log(data.data);
                            _this.planItemList=data.data;
                            // 输入替换备件编码，失去焦点时调接口带出备件名称
                            setTimeout(function(){
                                $("#repalceSpareCode").find("input").blur(function(){
                                    _this.replaceCodeBlur(_this.spareForm.replaceMaterialCode);
                                });
                            },100);
                        }else
                            // console.log(data.errorMessage);                             
                            _this.common.errorTip(data.errorMessage);
                    }
                });
            },
            // 替换备件弹窗中输入替换备件编号带出备件名称
            replaceCodeBlur(inputReplaceMaterialCode){
                var _this=this;
                if(inputReplaceMaterialCode && _this.spareForm.originalMaterialCode != inputReplaceMaterialCode){
                    var tmp={vendorId:_this.formData.vendorId,replaceMaterialCode:inputReplaceMaterialCode,planAggregationItemId:_this.planAggregationItemId};
                    $.ajax({
                        type: 'POST',
                        url: _this.procurement_service+"planAggregation/queryActivePriceInfoByMaterialCode",
                        cache: false,
                        data:JSON.stringify(tmp),
                        dataType:'JSON',
                        contentType: "application/json;charset=utf-8",     //*****
                        success: function (data) {
                            if (data.success) {
                                // console.log(data.data);
                                _this.spareForm.replaceMaterialName=data.data.replaceMaterialName;
                                _this.spareForm.topMostText=data.data.topMostText;
                                _this.spareForm.priority=data.data.priority;
                                _this.spareForm.replaceMaterialUnit=data.data.replaceMaterialUnit;
                                _this.spareForm.replaceMaterialSpecifications=data.data.replaceMaterialSpecifications;
                                _this.spareForm.replaceMaterialPrice=data.data.replaceMaterialPrice;
                            }else{
                                _this.common.errorTip(data.errorMessage);
                                if(data.errorCode == "procurement.planAggregation.material.not.exist.activePrice"){
                                    _this.spareForm.replaceMaterialName="";
                                    _this.spareForm.replaceMaterialUnit="";
                                    _this.spareForm.topMostText="";
                                    _this.spareForm.priorit="";
                                    _this.spareForm.replaceMaterialSpecifications="";
                                    _this.spareForm.replaceMaterialPrice="";
                                }
                            }
                        }
                    });
                }else{
                    _this.common.errorTip("您输入的信息有误，请重新输入！");
                    inputReplaceMaterialCode="";
                    return;
                }
            },
            // 输入替换数量
            replaceQuantity(index,row){
                var _this=this;
                 if(typeof(row.allottedQuantity) != 'number'){
                    _this.common.errorTip("请输入数字类型！");
                    row.allottedQuantity = "";
                }else if(row.allottedQuantity > row.maxQuantity){
                    _this.common.errorTip("替换的数量不能大于未采购数量！");
                    row.allottedQuantity="";
                }
                // for(var i=0;i<_this.planItemList.length;i++){
                //     if(_this.planItemList[i].allottedQuantity > _this.planItemList[i].maxQuantity){
                //         _this.planItemList[i].allottedQuantity="";
                //     }
                // }
            },
            //确认替换备件
            determineReplace(){
                var _this=this;
                _this.$refs["spareForm"].validate((valid) => {
                    if (valid) {
                        var planItemList=[],replaceQuantityFlag=false;   //替换数量为空，不能替换
                        if(_this.planItemList.length >= 1){
                            for(var i=0;i<_this.planItemList.length;i++){
                                if(typeof(_this.planItemList[i].allottedQuantity) == 'number' && _this.planItemList[i].allottedQuantity > 0){
                                    replaceQuantityFlag=true;   // 替换数量为数字且大于0，可以替换
                                    planItemList.push({
                                        planExecutableItemId:_this.planItemList[i].planExecutableItemId,
                                        planId:_this.planItemList[i].planId,
                                        planCode:_this.planItemList[i].planCode,
                                        planItemId:_this.planItemList[i].planItemId,
                                        replaceQuantity:_this.planItemList[i].allottedQuantity,     //替换数量
                                        planQuantity:_this.planItemList[i].quantity,      //计划数量
                                        maxQuantity:_this.planItemList[i].maxQuantity,      //未采购数量
                                    });
                                }else{
                                    _this.common.errorTip("请输入替换数量！");
                                } 
                            }
                        }
                        if(replaceQuantityFlag){    //替换数量有且仅有一个大于0 可以替换
                            var tmp={
                                planAggregationId:_this.planAggregationId,
                                planAggregationItemId:_this.planAggregationItemId,originalMaterialCode:_this.spareForm.originalMaterialCode,originalMaterialName:_this.spareForm.originalMaterialName,
                                originalMaterialUnit:_this.spareForm.unit,replaceMaterialCode:_this.spareForm.replaceMaterialCode,replaceMaterialName:_this.spareForm.replaceMaterialName,
                                priority:_this.spareForm.priority,
                                replaceMaterialUnit:_this.spareForm.replaceMaterialUnit,replaceMaterialSpecifications:_this.spareForm.replaceMaterialSpecifications,replaceMaterialPrice:_this.spareForm.replaceMaterialPrice,
                                vendorName:_this.formData.vendorName,
                                vendorId:_this.formData.vendorId,
                                planItemList:planItemList
                            };
                            $.ajax({
                                type: 'POST',
                                url: _this.procurement_service+"planAggregation/replaceMaterial",
                                cache: false,
                                data:JSON.stringify(tmp),
                                dataType:'JSON',
                                contentType: "application/json;charset=utf-8",     //*****
                                success: function (data) {
                                    if (data.success) {
                                        _this.replaceVisible = false;
                                        _this.common.successTip("备件替换成功！");
                                        _this.planSummaryQuery(_this.pageIndex, _this.pageSize);
                                        // _this.$refs['spareForm'].resetFields();
                                    } else {
                                        _this.common.errorTip(data.errorMessage);
                                    }
                                }
                            });
                        }else{
                            _this.common.errorTip("请输入替换数量！");
                        }
                        
                    } else {
                        _this.common.errorTip("存在未填写的必填项！");
                        return false;
                    }
                });
            },
            // 更换供应商
            replacementVendor(index,row){
                var _this=this;
                _this.planAggregationItemId=row.id;
                _this.replacementVendorVisible=true;
                _this.replacementVendorForm.supplierCode = "";
                _this.replacementVendorForm.supplierName = "";
                _this.supplierData=[];
                // 供应商列表
                var tmp1={planAggregationItemId:_this.planAggregationItemId,materialCode:row.materialCode};
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"planAggregation/openUpdateProviderInitProviders",
                    cache: false,
                    data:JSON.stringify(tmp1),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",     //*****
                    success: function (data) {
                        if (data.success) {
                            _this.supplierDataFull=data.data;
                            _this.supplierData = $.extend([], _this.supplierDataFull);
                        }else
                            _this.common.errorTip(data.errorMessage);
                    }
                });
                //  计划明细列表
                var tmp={planAggregationItemId:_this.planAggregationItemId};
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"planAggregation/queryPlanItemsByAggregationItemId",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",     //*****
                    success: function (data) {
                        if (data.success) {
                            _this.planDetailsData=data.data;
                            $(".replaceQuantity").find("input").val(0);
                            _this.changedAllQuantity=0;
                        }else
                            _this.common.errorTip(data.errorMessage);
                    }
                });
            },
            // 更改供应商中的查询
            queryVendor(){
                var _this=this;
                _this.supplierData = $.grep( _this.supplierDataFull, function(supplier,i){
                    var codeMatch = true,nameMatch = true;
                    if(_this.replacementVendorForm.supplierCode != ""){
                        if(supplier.partnerCode.indexOf(_this.replacementVendorForm.supplierCode) <= 0){
                            codeMatch = false;
                        }
                    }
                    if(_this.replacementVendorForm.supplierName != ""){
                        if(!supplier.vendorName.indexOf(_this.replacementVendorForm.supplierName) <= 0){
                            nameMatch = false;
                        }
                    }
                    return codeMatch && nameMatch;
                });
            },
            // 输入本次更改数量
            changedQuantity(index,row){
                var _this=this;
                if(typeof(row.replaceQuantity)!="number"){
                    _this.common.errorTip("本次更改数量只能为数字！");
                    row.replaceQuantity="";
                }else if(row.replaceQuantity > row.maxQuantity){
                    _this.common.errorTip("本次更改数量不能大于未采购数量！");
                    row.replaceQuantity="";
                }
                _this.changedAllQuantity=0;
                for(var i=0;i<_this.planDetailsData.length;i++){
                    _this.changedAllQuantity += _this.planDetailsData[i].replaceQuantity;
                    if(_this.planDetailsData[i].replaceQuantity > _this.planDetailsData[i].maxQuantity){
                        _this.planDetailsData[i].replaceQuantity="";
                    }
                }
            },
            // 给选中的供应商赋值----->本次更改的总数量
            allChangeQuantity(){
                var _this=this;
                for(var i=0;i<_this.supplierData.length;i++){
                    _this.supplierData[i].changedAllQuantity=0;
                    if(_this.selectSupplierId==_this.supplierData[i].id){
                        _this.supplierData[i].changedAllQuantity=_this.changedAllQuantity;
                        _this.activePrice = _this.supplierData[i].activePrice;
                        _this.selectSupplierName=_this.supplierData[i].partnerName;
                    }
                }
            },
            // 确定更改供应商
            confirmChangeSupplier(){
                var _this=this;
                // 给选中的供应商赋值    本次更改数量
                _this.allChangeQuantity();
                _this.confirmChangeSupplierPlanItemList=[];
                for(var i=0;i<_this.planDetailsData.length;i++){
                    if(_this.planDetailsData[i].replaceQuantity && _this.planDetailsData[i].replaceQuantity > 0){
                        _this.confirmChangeSupplierPlanItemList.push({
                            planCode:_this.planDetailsData[i].planCode,planExecutableItemId:_this.planDetailsData[i].planExecutableItemId,planId:_this.planDetailsData[i].planId,planItemId:_this.planDetailsData[i].planItemId,planQuantity:_this.planDetailsData[i].quantity,replaceQuantity:_this.planDetailsData[i].replaceQuantity,maxQuantity:_this.planDetailsData[i].maxQuantity
                        })
                    }else{
                        _this.common.errorTip("请填写本次更改数量！");
                        return;
                    }
                }
                var tmp={planAggregationId:_this.planAggregationId,planAggregationItemId:_this.planAggregationItemId,vendorName:_this.selectSupplierName,vendorId:_this.selectSupplierId,price:_this.activePrice,planItemList:_this.confirmChangeSupplierPlanItemList};
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"planAggregation/updatePartner",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",     //*****
                    success: function (data) {
                        if (data.success) {
                            _this.replacementVendorVisible=false;
                            _this.common.successTip("供应商替换成功！");
                            _this.supplierDataFull=data.data;
                            _this.supplierData = $.extend([], _this.supplierDataFull);
                            if(_this.continueBillingOpenSystemPOFlag){//继续开单
                                _this.supplierLists();     //供应商下拉列表中的值
                            }else{   //开制PO
                                _this.supplierLists1();     //供应商下拉列表中的值
                            }
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                });
            },
            //仅对 type=selection 的列有效，类型为 Function，Function 的返回值用来决定这一行的 CheckBox 是否可以勾选
            selectable(row, index){
                if(row.replaceStatus==1){
                    return false;
                }else{
                    return true;
                }
            },
            // selection-change	当选择项发生变化时会触发该事件
            handleSelectionChange(val){
                var _this=this;
                _this.planAggregationItemIds=[];
                for(var i=0;i<val.length;i++){
                    _this.planAggregationItemIds.push(val[i].id);
                    _this.vendorId=val[0].vendorId;
                }
            },
            // // 选择备件-----》给创建PO创造参数
            // selectMateraial(index,row){
            //     var _this=this;
            //     if($(event.target).parent().attr("class").indexOf("is-checked")!=-1){
            //         _this.planAggregationItemIds.push(row.id);
            //     }else{
            //         _this.planAggregationItemIds.pop(row.id);
            //     }
            //     _this.vendorId=row.vendorId;
            // },
            // 创建po
            createPO(){
                var _this=this;
                if(_this.planAggregationItemIds.length == 0){
                    _this.common.errorTip("请选择备件");
                }else{ 
                    // 当前选中备件的未采购数量必须大于0才可以创建PO
                    for(var i=0;i<_this.spareLists.length;i++){
                        if(_this.planAggregationItemIds.indexOf(_this.spareLists[i].id) > -1 && _this.spareLists[i].receivedQuantity > 0){
                            _this.$router.push({path:"/CreatePO",query:{partnerId:_this.vendorId,legalPersonCode:_this.formData.legalPersonCode,planAggregationItemIds:_this.planAggregationItemIds}});
                        }else{
                            _this.common.errorTip("该计划分配没有剩余未采购的数量，不能再次创建PO");
                        }
                    } 
                    // // var tmp={partnerId:_this.vendorId,legalPersonCode:_this.formData.legalPersonCode,planAggregationItemIds:_this.planAggregationItemIds};
                    // // $.ajax({
                    // //     type: 'POST',
                    // //     url: _this.procurement_service+"procurementOrder/validateCreatePo",
                    // //     cache: false,
                    // //     data:JSON.stringify(tmp),
                    // //     dataType:'JSON',
                    // //     contentType: "application/json;charset=utf-8",     //*****
                    // //     success: function (data) {
                    // //         if (data.success) {
                    //             _this.$router.push({path:"/CreatePO",query:{partnerId:_this.vendorId,legalPersonCode:_this.formData.legalPersonCode,planAggregationItemIds:_this.planAggregationItemIds}});
                    //             // "TJ001"_this.formData.legalPersonCode
                    // //         }else{
                    // //             _this.common.errorTip(data.errorMessage);
                    // //         }
                    // //     }
                    // // });
                }
            },
            //   分页
            handleSizeChange(val) {          //每页条数
                var _this=this;
                _this.pageSize=val;
                _this.planSummaryQuery(_this.pageIndex,_this.pageSize);
            },
            handleCurrentChange(val) {       // 当前页
                var _this=this;
                _this.pageIndex=val;
                _this.planSummaryQuery(_this.pageIndex,_this.pageSize);
            },
            // // ===================================
            // successTip(p) {     //成功时的提示信息
            //     this.$notify({
            //         title: '成功',
            //         message: p,
            //         type: 'success'
            //     });
            // },
            // errorTip(p) {     //错误提示
            //     this.$notify.error({
            //         title: '错误',
            //         message: p
            //     });
            // }
        }
    }
</script>